package com.woniu.customer.utils;

import com.woniu.customer.exception.ftpnoconnectionexception;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class FtpUtil {
    //  ftp地址
    @Value("${ftp.host}")
    private String host;
    //  ftp端口号
    @Value("${ftp.port}")
    private int port;
    //  ftp用户名
    @Value("${ftp.user}")
    private String username;
    //  ftp密码
    @Value("${ftp.password}")
    private String password;
    //  获取ftpClient
    public FTPClient getFtpClient() throws IOException {
        FTPClient ftpClient = new FTPClient();
        try{
            ftpClient.connect(host, port);
        }catch (IOException e){
            throw new ftpnoconnectionexception("ftp连接失败");
        }
        try{
            ftpClient.login(username, password);
        }catch (IOException e){
            throw new ftpnoconnectionexception("ftp登录失败");
        }
        try{
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        }catch (IOException e){
            throw new ftpnoconnectionexception("ftp设置文件失败");
        }
        return ftpClient;
    }
    //  上传文件
    public boolean uploadFile(MultipartFile file,String name,String formattedDate) throws IOException {
        //  获取路径  /上传时间
        String pathname = "/" + formattedDate;
        System.out.println(pathname);
        System.out.println("文件名2："+name);
        FTPClient ftpClient = getFtpClient();
        //  如果文件夹存在则切换至该目录，不存在则创建
        System.out.println(ftpClient.changeWorkingDirectory(formattedDate));
        System.out.println(ftpClient.changeWorkingDirectory(pathname));
        ftpClient.changeWorkingDirectory("/");
        if (ftpClient.changeWorkingDirectory(formattedDate)){
            System.out.println("未创建");
            InputStream inputStream = file.getInputStream();
            return ftpClient.storeFile(name, inputStream);
        } else {
            ftpClient.makeDirectory(formattedDate);
            ftpClient.changeWorkingDirectory(formattedDate);
            InputStream inputStream = file.getInputStream();
            return ftpClient.storeFile(name, inputStream);
        }
    }
    //  下载文件
    public boolean downloadFile(String fileName, OutputStream outputStream,String pathname) throws IOException {
        FTPClient ftpClient = getFtpClient();
        ftpClient.changeWorkingDirectory(pathname);
        return ftpClient.retrieveFile(fileName, outputStream);
    }
    //  删除文件
    public boolean deleteFile(String fileName,String pathname) throws IOException {
        FTPClient ftpClient = getFtpClient();
        ftpClient.changeWorkingDirectory(pathname);
        return ftpClient.deleteFile(fileName);
    }


}
